/*global $*/
/** Function to convert markerControl, polygonControl, and polylineControl
 *  data structures to GeoJSON object
 */
$.extend({
    geometryToGeoJSON: function(geometryControl) {
        var geojsonObjects = {}, types;

        types = {
            "Point": geometryControl.controls.markerControl.storage,
            "LineString": geometryControl.controls.polylineControl.storage,
            "Polygon": geometryControl.controls.polygonControl.storage
        };

        //Initialize geojsonObjects for point line and poly
        $.each(types, function(geoType,storage) {
            geojsonObjects[geoType] = {
                type: "FeatureCollection",
                features: []
            };
        });
        //Populate geojson.features
        //loop through the three main types (Point, Line, Poly)
        $.each(types, function(geoType, storage) {
            //storage is an array of GeometryControls#beans#Geometry

            //loop through all the values of that geoType
            $.each(storage, function(garbage, poly) {
                //check if poly has null value due to deletion
                if (poly !== null) {
                    var latLng, feature, geometry = poly.geometry, i;

                    feature = {
                        type: "Feature",
                        geometry: {
                            type: geoType
                        },
                        properties: {
                            title: poly.title[0],
                            description: poly.description[0]
                        }
                    };
                    //poly could be of type GMarker, GPolyline, or GPolygon
                    if (geoType === "Point") {
                        //GMarker's GLatLng directly stored
                        latLng = geometry.getLatLng();
                        feature.geometry.coordinates = [latLng.lng(), latLng.lat()];
                    } else if (geoType === "LineString" || geoType === "Polygon") {
                        //GPolyline and GPolygon have GLatLng's only accessable
                        //with .getVertex method
                        feature.geometry.coordinates = [];
                        for (i = 0; i < geometry.getVertexCount(); i += 1) {
                            latLng = geometry.getVertex(i);
                            feature.geometry.coordinates.push([latLng.lng(), latLng.lat()]);
                        }
                        if (geoType === "Polygon") {
                            //must be an array of LinearRing coordinate arrays,
                            //subsequent elements represent interior rings, but that
                            //will not happen with our simplistic tool that only allows
                            //single polys to be input
                            feature.geometry.coordinates = [feature.geometry.coordinates];
                        }
                    } else {
                        throw "Unknown geoType: " + geoType;
                    }
                    geojsonObjects[geoType].features.push(feature);
                }
            });
        });
        return geojsonObjects;
    }
});